
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author rohit
 */
public class InsertThread2 extends MainGui implements Runnable{
    
    int first;
    int last;
    Connection c;

    public InsertThread2(int firstInsert, int lastInsert, Connection c) {
        this.first = firstInsert;
        this.last = lastInsert;
        this.c = connection;
        establishConnection();
    }
    
    @Override
    public void establishConnection() {
        System.out.println("-------- PostgreSQL "
                + "JDBC Connection Testing ------------");

        if (connection != null) {
            return;
        }
        try {
            Class.forName("org.postgresql.Driver");
            connection = DriverManager.getConnection(
                    "jdbc:postgresql://localhost:5432/test", "rohit",
                    "d3fad7d3");
            if (connection != null) {
                System.out.println("Connecting to database...");
            }
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    @Override
    public void startTx() {
        PreparedStatement prep = null;
        try {
            prep = connection.prepareStatement("begin transaction;");
            prep.executeUpdate();
            //System.out.println("begin transaction");
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    @Override
    public void setEnvironment() {
        PreparedStatement prep = null;
        try {
            prep = connection.prepareStatement("set enable_seqscan = off");
            prep.executeUpdate();
            prep = connection.prepareStatement("set enable_bitmapscan = off");
            prep.executeUpdate();
            System.out.println("Environment variable set");
        } catch (Exception e) {
            System.out.println("Problem in setting the environment variables");
        }
    }

    @Override
    public void endTx() {
        PreparedStatement prep = null;
        try {
            prep = connection.prepareStatement("end transaction;");
            prep.executeUpdate();
           // System.out.println("End transaction");
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    
    @Override
    public PreparedStatement insertDataMBII(int firstInsert, int lastInsert) {
        PreparedStatement prep = null;
        int valueOfId;
        try {
            connection.setAutoCommit(false);
            for (valueOfId = firstInsert; valueOfId < lastInsert; valueOfId++) {
                prep = connection.prepareStatement("INSERT INTO emp(name,id) values(?,?);");
                prep.setInt(2, valueOfId);
                prep.setString(1, "rohit");
                prep.executeUpdate();
                if(valueOfId%1000==0)
                {
                    new Thread(new ReadingThread(2, valueOfId, connection)).start();
                }
                // System.out.println("Insertion works fine");
            }
            System.out.println("Insertion works fine");
            connection.commit();
        } catch (Exception e) {
            System.out.println("Problem in inserting the database"+e);
        }
        return prep;
    }


 
    
    @Override
    public void run() {
        insertDataMBII(first, last);
    }           
}

    
